package com.hankol.message.adapter

import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import com.baselibrary.extension.checkValue
import com.baselibrary.utils.GlideUtils
import com.baselibrary.utils.TimeUtil
import com.baselibrary.view.RoundCornerImageView
import com.baselibrary.view.VipHeardImgView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.message.R
import com.hankol.message.bean.CommentMeBean
import com.like.LikeButton
import com.like.OnLikeListener

/*评论我的
* */
class CommentMeAdapter : BaseQuickAdapter<CommentMeBean, BaseViewHolder>(R.layout.item_comment_me) {
    interface LikeCallBack {
        fun likeClick(pos: Int)
        fun unLikeClick(pos: Int)
    }

    lateinit var callback: LikeCallBack
    fun setLikeCallBack(callback: LikeCallBack) {
        this.callback = callback
    }

    override fun convert(helper: BaseViewHolder, item: CommentMeBean) {
        helper.apply {
            //头像
            getView<VipHeardImgView>(R.id.img_heard).setData(item.icon.checkValue(), item.isFans == 1)
            //时间
            setText(R.id.tv_option_time, TimeUtil.fromToday(item.commentTime))
            //昵称
            setText(R.id.tv_nick, item.name.checkValue())
            //是否好友
            setGone(R.id.tv_tag_friend, item.isFriend == 1)
            setText(R.id.tv_option_tip, showTipStr(item.btype))
            //评论内容 & 回复内容
            if (item.btype == 1) {//评论了我的动态  只显示评论内容
                setText(R.id.tv_comment, item.taCommentContent.checkValue())
                getView<TextView>(R.id.tv_comment).maxLines = 3
                getView<TextView>(R.id.tv_comment).setEllipsize(TextUtils.TruncateAt.END)
            } else if (item.btype == 2) { //回复了我的评论
                setText(R.id.tv_comment, item.taCommentContent.checkValue())
                getView<TextView>(R.id.tv_comment).maxLines = 1
                getView<TextView>(R.id.tv_comment).setEllipsize(TextUtils.TruncateAt.END)
            }
            //是否显示对方的评论内容
            setGone(R.id.ll_cite, item.btype == 2)
            //这里显示TA的评论
            setText(R.id.tv_cite_comment, if (item.btype == 1) item.taCommentContent.checkValue() else item.myCommentContent.checkValue())
            //是否 视频动态
            setGone(R.id.img_is_video, item.timelineShowtype == 2)
            //封面图
            GlideUtils.loadImg_101(mContext, item.timelineCover.checkValue(), getView<RoundCornerImageView>(R.id.riv_cover))
            //回复评论
            addOnClickListener(R.id.img_heard)
            addOnClickListener(R.id.ll_nick)
            addOnClickListener(R.id.tv_reply)
            //是否赞过
            val imgLike = getView<LikeButton>(R.id.img_like)
            imgLike.isLiked= item.isSupport == 1
            //点赞
            imgLike.setOnLikeListener(object : OnLikeListener {
                override fun liked(likeButton: LikeButton?) {
                    imgLike.isLiked=true
                    callback.likeClick(helper.adapterPosition)
                }

                override fun unLiked(likeButton: LikeButton?) {
                    imgLike.isLiked=false
                    callback.unLikeClick(helper.adapterPosition)
                }
            })
        }
    }

    fun showTipStr(type: Int): String {
        var str = ""
        when (type) {
            1 -> {
                str = "评论了我的动态"
            }
            2 -> {
                str = "回复了我的评论"
            }
        }
        return str
    }
}